const util = require('../../../../utils/util.js')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    youxiaoqishow:"",
		logisticsStatusItems: [
		
      '7天',
      '15天',
      '30天'
      
		],
    lang: 'zh_CN',
    value1: [],
    selectedValue:"",
    value2: [],
    displayValue1: '放券开始时间',
    displayValue2:"放券结束时间",
    couponname:"",//优惠券名称
    startTime:"",//优惠券发放开始时间
    deadline:"",//优惠券发放开始时间
    youxiaoqi: null,//有效期
    limitedreceive:"",//限领张数
    moneyHreshold:"",//满多少钱
    preferentialamount:"",//优惠金额
    issuedquantity:""//发放数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var dd  = new Date()
    var year = dd.getFullYear()
    var month = dd.getMonth()
    var day = dd.getDate()
  
    var hour = dd.getHours()
    var minute = dd.getMinutes()
    this.setData({
      value1:[year, month, day,hour,minute],
      value2:[year, month, day,hour,minute]
    })
  console.log(this.data.value1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  //优惠券保存
  addcoupons(e){
      
    var that=this
    if(!that.data.couponname){
      wx.showModal({
        title: '提示',
        content: '请填写优惠券名称',
      })
      return
    }
    else  if(!that.data.startTime ){
      wx.showModal({
        title: '提示',
        content: '请选择开始时间',
      })
      return
    }else if(!that.data.deadline   ){
      wx.showModal({
        title: '提示',
        content: '请选择结束时间',
      })
      return
    }else if(!that.data.limitedreceive  ){
      wx.showModal({
        title: '提示',
        content: '请设置限领张数',
      })
      return
    }else if(!that.data.youxiaoqi  ){
      wx.showModal({
        title: '提示',
        content: '请选择券有效期',
      })
      return
    }
    
    else if(!that.data.moneyHreshold ){
      wx.showModal({
        title: '提示',
        content: '请输入满多少钱',
      })
      return
    }
    else if(!that.data.preferentialamount ){
      wx.showModal({
        title: '提示',
        content: '请输入减多少钱',
      })
      return
    }
    else if(!that.data.issuedquantity ){
      wx.showModal({
        title: '提示',
        content: '请输入发放数量',
      })
      return
    }else{
      const db = wx.cloud.database()
      db.collection('couponsMarket').add({
          data: {
            dateAdd:new Date(), 
            couponname:that.data.couponname,//优惠券名称
            startTime:that.data.startTime,//优惠券发放开始时间
            deadline:that.data.deadline,//优惠券发放开始时间
            youxiaoqi: parseInt(that.data.youxiaoqi),//有效期
            limitedreceive:that.data.limitedreceive,//限领张数
            moneyHreshold:parseInt(that.data.moneyHreshold),//满多少钱
            preferentialamount:parseInt(that.data.preferentialamount),//优惠金额
            issuedquantity:that.data.issuedquantity,//发放数量
            status: 1,//生效中
          },
          success: function(res) {
          //  console.log(res)
          wx.navigateBack({
            delta: 2,
            
          })
        },
          fail: err => {
                console.log(err)
          }
      })
    }
    
  },
  //优惠券名称值绑定
  couponnameInput(e){
    this.data.couponname = e.detail.value
  },
  //优惠券有效期设置
  youxiaoqiChange(e) {
    if(e.detail.value==0){
      this.setData({
        youxiaoqi: 7//有效期7天
      })
    }
    else if(e.detail.value==1){
      this.setData({
        youxiaoqi: 15
      })
    }
    else if(e.detail.value==2){
      this.setData({
        youxiaoqi: 30
      })
    }
    this.setData({
      youxiaoqishow: e.detail.value
    })
    
    console.log(this.data.youxiaoqi)
  },
  //时间组件改变
  onConfirm(e) {
    const { index, mode } = e.currentTarget.dataset
    this.setValue(e.detail, index, mode)
    console.log(`onConfirm${index}`, e.detail)
    
    console.log(util.formatTime(util.stringToDate(e.detail.label+':00')))
    if(index ==1){
      //设置开始时间
      this.setData({
        startTime: util.stringToDate(e.detail.label+':00')
      })
    }else if(index==2){
       //设置截止时间
      this.setData({
        deadline: util.stringToDate(e.detail.label+':59')
      })
      
    }
  },
  onChange(e) {
    console.log(e)
    this.setData({
      limitedreceive: e.detail.value,
    })
  },
  //时间选择值改变触发
  onVisibleChange(e) {
    this.setData({ visible: e.detail.visible })
  },
  //时间组件赋值
  setValue(values, key, mode) {
    this.setData({
        [`value${key}`]: values.value,
        [`displayValue${key}`]: values.label,
        // [`displayValue${key}`]: values.displayValue.join(' '),
    })
  },
  //满多少金额值改变触发
  moneyHresholdInput(e){
    
    this.setData({
      moneyHreshold:  e.detail.value
    })
    console.log(e.detail.value)
  },
  //减多少钱值改变触发
  preferentialamountInput(e){
    this.setData({
      preferentialamount:  e.detail.value
    })
    console.log(this.data.preferentialamount)
  },
  //发放数量值改变触发
  issuedquantityChange(e){
    this.setData({
      issuedquantity: e.detail.value,
    })
  },
  /**
   * onClick() {
    this.setData({ visible: true })
  },
  */
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})